<?php
/**
 * Created by PhpStorm.
 * User: zhyg
 * Date: 2015/8/20
 * Time: 10:38
 * 分页类
 */
class Page{
    private $page_length;
    private $page_count;    //列表总记录数
    private $page_all;
    private $page_current;
    private $page_pre;
    private $page_next;
    private $uri;
    private $page_string;   //显示在模板的键接按扭字符串，选择上下页
    public $limit_offset;


    /**
     * @param $table_name 不加前缀的表名
     * @param $page_length 规定每页显示数目
     * @param $uri          请求的哪个脚本位置或对应某个类的某个方法
     * @param $page_count  查询条件查出的所有记录数
     */
    public function __construct($uri,$page_length,$page_count){

       // $this->table_name = $table_name;
        $this->page_length = $page_length;
        $this->page_count = $page_count;
        $this->uri = $uri;
        $this->pageAll();
        $this->page_current=isset($_REQUEST['page'])?min(max($_REQUEST['page'],1),$this->page_all):1;
        $this->page_pre = $this->page_current-1;
        $this->page_next = $this->page_current+1;
        $this->limitOffset();
    }

    /*
     *  产生分页按扭的公开方法，供外部对象调用
     * @return String html内容
     * */
    public function pageString(){
        $this->page_string .="<style>.page-me,#pagenum{float:left;margin-right:5px;}a.page-me,input.page-me{text-decoration:none;color:whitesmoke;background-color: #3399ff;padding:2px;border-radius:5px;}#pagenum{width:30px;}a.page-me:hover{background-color: #2B8BB0;}</style>";
        $this->page_string .= "<a class='page-me' href='$this->uri&page=1'>首页</a>";
        $this->page_string .= "<a class='page-me' href='$this->uri&page={$this->page_pre}'>上一页</a>";
        $this->page_string .= "<a class='page-me' href='$this->uri&page={$this->page_next}'>下一页</a>";
        $this->page_string .= "<a class='page-me' href='$this->uri&page=$this->page_all'>末页</a>";
        $this->page_string .="<form class='page-me' action='$this->uri' method='post'><input id='pagenum' type='text' name='page' value='$this->page_current' /><input type='submit' class='page-me' value='跳转' /></form>";
        return $this->page_string;
    }

    /**<input type='hidden' name='act' value='test' />
     *计算分页总页数
     */
    private function pageAll(){
        $this->page_all = ceil($this->page_count/$this->page_length);

    }

    /**
     *计算出查询语句中的limit第一个参数
     */
    private function limitOffset(){
         $this->limit_offset = ($this->page_current-1)*$this->page_length;
    }

}